package com.hongguan.oms.web.User;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongguan.oms.web.BaseHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.hongguan.oms.common.Const;
import com.hongguan.oms.domain.UserInfo;
import com.hongguan.oms.service.UserInfoService;

public class UserManagement extends BaseHandler {
	private UserInfoService service;

	public void setService(UserInfoService service) {
		this.service = service;
	}
	
	
	//EditUser
	public ModelAndView updateUser(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		
		ModelAndView mv = null;
		mv = this.getSuccessModelView();

		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Date date = new Date();
			
			if (request.getParameter("userName").equals("")) {
			} else {
				map.put("userName", request.getParameter("userName"));
			}
			if (request.getParameter("userId").equals("")) {
			} else {
				map.put("userId", request.getParameter("userId"));
			}
			if (request.getParameter("id").equals("")) {
			} else {
				map.put("id", request.getParameter("id"));
			}
			
			
			map.put("updatedate", new SimpleDateFormat("yyyy-MM-dd")
			.format(date));
			service.updateUser(map);
			
			if (request.getParameter("RoleId").equals("")) {
				map.put("RoleId", "0");
			} else {
				map.put("RoleId", request.getParameter("RoleId"));
			}
			
			service.updateUser_Role(map);
			Map  departList=service.getDepartNumByRole(map);
			
			
			map.put("DepartID", departList.get("DEPARTMENT_ID"));
			service.updateUserDepart(map);
			
			mv.addObject(SUCCESS, "true");
			}
		catch(Exception e){
			e.printStackTrace();
			mv.addObject(ERROR, "false");
		}
	
		
		
		return mv;
		
	}
	
	//CreateUser
	public ModelAndView addUser(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		 
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Date date = new Date();
			
			if (request.getParameter("userName").equals("")) {
			} else {
				map.put("userName", request.getParameter("userName"));
			}
			if (request.getParameter("userId").equals("")) {
			} else {
				map.put("userId", request.getParameter("userId"));
			}
			if (request.getParameter("passWord").equals("")) {
			} else {
				map.put("passWord", request.getParameter("passWord"));
			}
			map.put("createdate", new SimpleDateFormat("yyyy-MM-dd")
			.format(date));
			service.addUser(map);
			//取出最大的userID
			Long maxUserID= service.getUserMaxID(map);
			
			map.put("maxUserID", maxUserID);
			//抓取 RoleID
			if (request.getParameter("RoleId").equals("")) {
				map.put("RoleId", "0");
				map.put("DepartID", "0");
			} else {
				map.put("RoleId", request.getParameter("RoleId"));
				Map  departList=service.getDepartNumByRole(map);
				map.put("DepartID", departList.get("DEPARTMENT_ID"));
			}
			service.addUser_Role(map);
			service.addUserDepart(map);
			mv.addObject(SUCCESS, "true");
			}
		catch(Exception e){
			e.printStackTrace();
			mv.addObject(ERROR, "false");
		}
		
		
		return mv;
	}
	//部门查找2-25
	
	public ModelAndView QueryDepart(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
//		ModelAndView mv = null;
//		mv = this.getSuccessModelView();
		
		Map<String, Object> param = new HashMap<String, Object>();
		String departName = request.getParameter("departName");
		String departNum = request.getParameter("departNum");

		param.put("departName", departName);
		param.put("departNum", departNum);
		
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		start = start == null ? "0" : start;
		limit = limit == null ? "100" : limit;
		int beginRow = Integer.parseInt(start);
		int endRow = Integer.parseInt(limit); //+ beginRow;
		param.put("beginRow", beginRow);
		param.put("endRow", endRow);
		
		List list= service.getDepartBySearchInfo(param);
		int i=0;
		String[][] str=new String[list.size()][5];
		Integer DEPARTMENT_ID;
		Integer DOWNDEPART_ID;
		
		for (i=1;i<=list.size();i++){
			Map map=(Map) list.get(i-1);
			String[] strIn=new String[5];
			//strIn[0]=(String) map.get("DEPARTMENT_ID");//strIn[0]=(String) map.get("DEPARTMENT_ID");
			//林翊 修改
			DEPARTMENT_ID=(Integer)map.get("DEPARTMENT_ID");
			strIn[0]=DEPARTMENT_ID.toString();
			
			
			strIn[1]=(String) map.get("DEPARTMENT_NAME");
			//strIn[2]=(String) map.get("ID");
			strIn[3]=(String) map.get("DOWNDEPART_NAME");
			
			//林翊 修改
			DOWNDEPART_ID=(Integer) map.get("DOWNDEPART_ID");
			strIn[4]=DOWNDEPART_ID.toString();
			//, ,'','',''
			str[i-1]=strIn;
		}
		ModelAndView mv = new ModelAndView("jsonHtmlView");
		mv.addObject("result", str);
		mv.addObject("total", service.getDepartCount(param));
		mv.addObject(SUCCESS, "true");
		return mv;

	}
	
	public ModelAndView updateDepart(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		
		ModelAndView mv = null;
		mv = this.getSuccessModelView();

		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Date date = new Date();
			
			//EditDepartName: EditDepartName.getValue(),//部门名称
			//EditDepartNum: EditDepartNum.getValue(),//上级部门ID
			//id: EditId.getValue()//部门ID
			
			if (request.getParameter("EditDepartNum").equals("")) {
				map.put("parent_id", "1");
			} else {
				map.put("parent_id", request.getParameter("EditDepartNum"));
			}
			
			if (request.getParameter("EditDepartName").equals("")) {
			} else {
				map.put("EditDepartName", request.getParameter("EditDepartName"));
			}
			
			if (request.getParameter("id").equals("")) {
			} else {
				map.put("id", request.getParameter("id"));
			}
			
			map.put("updatedate", new SimpleDateFormat("yyyy-MM-dd")
			.format(date));
			service.updateDepart(map);
			
			//DepartID
			
			
			mv.addObject(SUCCESS, "true");
			}
		catch(Exception e){
			e.printStackTrace();
			mv.addObject(ERROR, "false");
		}
	
		
		
		return mv;
		
	}
	

	public ModelAndView QueryUser(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		
		Map<String, Object> param = new HashMap<String, Object>();
		String username = request.getParameter("username");
		String usernum = request.getParameter("usernum");
		String usertel = request.getParameter("usertel");
		String useremail = request.getParameter("useremail");

		param.put("username", username);
		param.put("usernum", usernum);
		param.put("usertel", usertel);
		param.put("useremail", useremail);
		
		
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		start = start == null ? "0" : start;
		limit = limit == null ? "10" : limit;
		int beginRow = Integer.parseInt(start);
		int endRow = Integer.parseInt(limit); //+ beginRow;
		param.put("beginRow", beginRow);
		param.put("endRow", endRow);
		mv.addObject("result", service.getUserBySearchInfo(param));
		mv.addObject("total", service.getUserCount(param));
		return mv;

	}

	public ModelAndView DelUser(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {

		// List<UserInfo> items = addItem(request, UserInfo.class);
		// System.out.println(items.size());
		System.out.println("-------------------------------------");
		String username = request.getParameter("ids");
		System.out.println(username);
		String[] usernameArray = username.split(",");

		for (int i = 0; i < usernameArray.length; i++) {
			
			System.out.println(usernameArray[i]);
			
		}

		ModelAndView mv = this.getSuccessModelView();
		return mv;

	}
	
	public ModelAndView addDepart(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			
			Date date = new Date();
			
			if (request.getParameter("departName").equals("")) {
			} else {
				map.put("departName", request.getParameter("departName"));
			}
			
			if (request.getParameter("UpDepartName").equals("")) {
				map.put("parent_id", "1");
			} else {
				map.put("parent_id", request.getParameter("UpDepartName"));
			}
			
			
			
			Long maxid;
			maxid=(service.getMaxDepartId())+1;
			
			map.put("departId",maxid);
			
			map.put("createdate", new SimpleDateFormat("yyyy-MM-dd")
			.format(date));
			service.addDepart(map);
			
			mv.addObject(SUCCESS, "true");
			}
		catch(Exception e){
			e.printStackTrace();
			mv.addObject(ERROR, "false");
		}
		
		
		return mv;
	}
	
	public ModelAndView QueryRole(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		
		Map<String, Object> param = new HashMap<String, Object>();
		String username = request.getParameter("username");
		String usernum = request.getParameter("usernum");
		String usertel = request.getParameter("usertel");
		String useremail = request.getParameter("useremail");

		param.put("username", username);
		param.put("usernum", usernum);
		param.put("usertel", usertel);
		param.put("useremail", useremail);
		
		
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		start = start == null ? "0" : start;
		limit = limit == null ? "10" : limit;
		int beginRow = Integer.parseInt(start);
		int endRow = Integer.parseInt(limit); //+ beginRow;
		param.put("beginRow", beginRow);
		param.put("endRow", endRow);
		mv.addObject("result", service.getRoleBySearchInfo(param));
		mv.addObject("total", service.getRoleCount(param));
		return mv;

	}
	//2011-3-1新建
	//2011-3-15 修改
	public ModelAndView addRole(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			
			Date date = new Date();
			
			
			
			
			if (request.getParameter("RoleName").equals("")) {
			} else {
				map.put("RoleName", request.getParameter("RoleName"));
			}
			if (request.getParameter("DepartId").equals("")) {
			} else {
				map.put("departId", request.getParameter("DepartId"));
			}
			map.put("createdate", new SimpleDateFormat("yyyy-MM-dd")
			.format(date));
			
			service.addRole(map);
			
			Long roleCount = service.getRoleCount(map);
			map.put("Role_Id", roleCount);
			
			if (request.getParameter("grgzt").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("grgzt"));
				service.addRolePermiss(map);
			}
			
			if (request.getParameter("glgzt").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("glgzt"));
				service.addRolePermiss(map);
			}
			
			if (request.getParameter("htgl").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("htgl"));
				service.addRolePermiss(map);
			}
			
			if (request.getParameter("rb").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("rb"));
				service.addRolePermiss(map);
			}
			if (request.getParameter("xxyh").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("xxyh"));
				service.addRolePermiss(map);
			}
			
			if (request.getParameter("zlxz").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("zlxz"));
				service.addRolePermiss(map);
			}
			if (request.getParameter("yhsz").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("yhsz"));
				service.addRolePermiss(map);
			}
			if (request.getParameter("bmsz").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("bmsz"));
				service.addRolePermiss(map);
			}
			if (request.getParameter("jssz").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("jssz"));
				service.addRolePermiss(map);
			}
			
			
			mv.addObject(SUCCESS, "true");
			}
		catch(Exception e){
			e.printStackTrace();
			mv.addObject(ERROR, "false");
		}
		return mv;
	}
	
//	3-15 修改 有问题
	public ModelAndView updateRole(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Date date = new Date();
			if (request.getParameter("EditRoleName").equals("")) {
			} else {
				map.put("EditRoleName", request.getParameter("EditRoleName"));
			}
			if (request.getParameter("editDepartName").equals("")) {
			} else {
				map.put("editDepartName", request.getParameter("editDepartName"));
			}
			if (request.getParameter("id").equals("")) {
			} else {
				map.put("id", request.getParameter("id"));
				map.put("Role_Id", request.getParameter("id"));
			}
			map.put("updatedate", new SimpleDateFormat("yyyy-MM-dd")
			.format(date));
			service.updateRole(map);
			
			List  USER_ID = service.getUserIdFromSysUserRoleByRoleID(map);
			
			int mapLength=USER_ID.size();
			
			for(int i=0;i<mapLength;i++){
				map.put("USER_ID", USER_ID.get(i));
				
				System.out.println(map);
				System.out.println(map.get("USER_ID"));
				
				service.updateUserDepartByUserID(map);
				
			}
			
			
			service.delRolePermiss(map);
			//--------------------------------
			
			if (request.getParameter("grgzt").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("grgzt"));
				service.addRolePermiss(map);
			}
			
			if (request.getParameter("glgzt").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("glgzt"));
				service.addRolePermiss(map);
			}
			
			if (request.getParameter("htgl").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("htgl"));
				service.addRolePermiss(map);
			}
			
			if (request.getParameter("rb").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("rb"));
				service.addRolePermiss(map);
			}
			if (request.getParameter("xxyh").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("xxyh"));
				service.addRolePermiss(map);
			}
			
			if (request.getParameter("zlxz").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("zlxz"));
				service.addRolePermiss(map);
			}
			if (request.getParameter("yhsz").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);;
			}else{
				map.put("PERMISS_ID", request.getParameter("yhsz"));
				service.addRolePermiss(map);
			}
			if (request.getParameter("bmsz").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("bmsz"));
				service.addRolePermiss(map);
			}
			if (request.getParameter("jssz").equals("")) {
				map.put("PERMISS_ID", "0");
				service.addRolePermiss(map);
			}else{
				map.put("PERMISS_ID", request.getParameter("jssz"));
				service.addRolePermiss(map);
			}
			
			mv.addObject(SUCCESS, "true");
			}
		
		catch(Exception e){
			e.printStackTrace();
			mv.addObject(ERROR, "false");
		}
		return mv;
		
	}
	//3-15 新增
	public ModelAndView QueryRoleAndPermiss(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("RoleId", request.getParameter("RoleId"));
		mv.addObject("result", service.getRoleAndPermiss(param));
		return mv;

	}
	//3-28
	public ModelAndView QueryDepart2(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		
		Map<String, Object> param = new HashMap<String, Object>();
		String departName = request.getParameter("departName");
		String departNum = request.getParameter("departNum");

		param.put("departName", departName);
		param.put("departNum", departNum);
		
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		start = start == null ? "0" : start;
		limit = limit == null ? "100" : limit;
		int beginRow = Integer.parseInt(start);
		int endRow = Integer.parseInt(limit); //+ beginRow;
		param.put("beginRow", beginRow);
		param.put("endRow", endRow);
		
		
		
		mv.addObject("result", service.getDepart(param));
		mv.addObject("total", service.getDepart2Count(param));
		mv.addObject(SUCCESS, "true");
		return mv;

	}

}
